package com.winchain.chainbill.bill.biz.discount.service.impl;

import com.winchain.chainbill.basic.context.TimeoutOperation;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillEvent;
import com.winchain.chainbill.bill.biz.contract.rx.ContractSignSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctInsAcrSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctPaidConfirmMsgSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctPaidConfirmSmsSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctPmtPassSubscriber;
import com.winchain.chainbill.bill.biz.discount.service.BillDctPmtService;
import com.winchain.chainbill.bill.biz.discount.vo.BillDct;
import com.winchain.chainbill.bill.biz.send.rx.BillMoveSubscriber;
import com.winchain.chainbill.bill.biz.send.rx.BillOptSubscriber;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("billDctPmtService")
public class BillDctPmtServiceImpl implements BillDctPmtService {
	@Autowired
	private CbDctService cbDctService;
	@Autowired
	private BillOptSubscriber billOptSubscriber;
	@Autowired
	private BillMoveSubscriber billMoveSubscriber;
	@Autowired
	private BillDctPmtPassSubscriber billDctPmtPassSubscriber;
	@Autowired
	private BillDctPaidConfirmMsgSubscriber billDctPaidConfirmMsgSubscriber;
	@Autowired
	private BillDctPaidConfirmSmsSubscriber billDctPaidConfirmSmsSubscriber;
	@Autowired
	private ContractSignSubscriber<CbDct> contractSignSubscriber;
	@Autowired
	private BillDctInsAcrSubscriber billDctInsAcrSubscriber;

	public void pmt(String phoneCode, String billId) {
		BillDct billDct = this.cbDctService.selectBillDctByBillIdAndState(billId, "1");

		billDct.setPmtState("1");
		billDct.setDctState("2");

		CbDct dct = billDct.getDct();
		Bill<CbDct> bill = new Bill();
		bill.setRec(billDct.getRec());
		bill.setSource(billDct.getDct());
		((CbDct) bill.getSource()).setDctQtOpt("2");

		CbOpt cbOpt = new CbOpt();
		cbOpt.setCurState("S011");
		cbOpt.setOptCode("E303");
		cbOpt.setRemark("����������������");

		TimeoutOperation timeoutOpt = new TimeoutOperation();
		timeoutOpt.setOptCode("E004");
		timeoutOpt.setRemark("������������,������������");

		BillEvent<CbDct> event = new BillEvent(bill);
		event.setOperation(cbOpt);
		event.setTimeoutOpt(timeoutOpt);
		event.setValue("phoneCode", phoneCode);
		Observable<BillEvent<CbDct>> observers = Observable.just(event);
		if (!dct.getSellerId().equals(dct.getBuyerId())) {
			observers.subscribe(this.contractSignSubscriber);
		}
		observers.subscribe(this.billOptSubscriber);

		observers.subscribe(this.billMoveSubscriber);
		observers.subscribe(this.billDctPmtPassSubscriber);
		observers.subscribe(this.billDctInsAcrSubscriber);

		observers.subscribe(this.billDctPaidConfirmMsgSubscriber);

		observers.subscribe(this.billDctPaidConfirmSmsSubscriber);
	}

	public void pmt(String phoneCode, String billId, String pmtDate, BigDecimal recvAmt, BigDecimal dctInt,
			String dctDays) {
		BillDct billDct = this.cbDctService.selectBillDctByBillIdAndState(billId, "1");

		billDct.setPmtState("1");
		billDct.setDctState("2");

		CbDct dct = billDct.getDct();
		Bill<CbDct> bill = new Bill();
		bill.setRec(billDct.getRec());
		bill.setSource(billDct.getDct());
		((CbDct) bill.getSource()).setDctQtOpt("2");

		CbOpt cbOpt = new CbOpt();
		cbOpt.setCurState("S011");
		cbOpt.setOptCode("E303");
		cbOpt.setRemark("����������������");

		TimeoutOperation timeoutOpt = new TimeoutOperation();
		timeoutOpt.setOptCode("E004");
		timeoutOpt.setRemark("������������,������������");

		BillEvent<CbDct> event = new BillEvent(bill);
		event.setOperation(cbOpt);
		event.setTimeoutOpt(timeoutOpt);
		event.setValue("phoneCode", phoneCode);
		event.setValue("pmtDate", pmtDate);
		event.setValue("recvAmt", recvAmt);
		event.setValue("dctInt", dctInt);
		event.setValue("dctDays", dctDays);
		Observable<BillEvent<CbDct>> observers = Observable.just(event);
		if (!dct.getSellerId().equals(dct.getBuyerId())) {
			observers.subscribe(this.contractSignSubscriber);
		}
		observers.subscribe(this.billOptSubscriber);

		observers.subscribe(this.billMoveSubscriber);
		observers.subscribe(this.billDctPmtPassSubscriber);
		observers.subscribe(this.billDctInsAcrSubscriber);

		observers.subscribe(this.billDctPaidConfirmMsgSubscriber);

		observers.subscribe(this.billDctPaidConfirmSmsSubscriber);
	}
}
